package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goal;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class GDao {

	DBHelp<Goal> db=new DBHelp<Goal>();
	
	public void save(Goal goal){
		String sql="insert into t_goal(id,gname,gdescripe,gcreatetime,projectid) values(?,?,?,?,?)";
		db.executeSQL(sql,goal.getId(),goal.getGname(),goal.getGdescripe(),goal.getGcreatetime(),goal.getProjectid());
	}
	
	public void update(Goal goal){
		String sql="update t_goal set gname=?,gdescripe=? where id=?";
		db.executeSQL(sql,goal.getGname(),goal.getGdescripe(),goal.getId());
	}
	
	public Goal findById(String id){
		String sql="select id,gname,gdescripe,gcreatetime,projectid from t_goal where id=?";
		return db.queryForObject(sql,new GRowMapper(),id);
	}
	
	public List<Goal> findByProjectid(String projectid){
		String sql="select id,gname,gdescripe,gcreatetime,projectid from t_goal where projectid=?";
		return db.queryForList(sql,new GRowMapper(),projectid);
	}
	
	private class GRowMapper implements RowMapper<Goal>{

		public Goal mapperRow(ResultSet rs) throws SQLException {
			Goal goal=new Goal();
			goal.setGcreatetime(rs.getString("gcreatetime"));
			goal.setGdescripe(rs.getString("gdescripe"));
			goal.setGname(rs.getString("gname"));
			goal.setId(rs.getString("id"));
			goal.setProjectid(rs.getString("projectid"));
			return goal;
		}
		
	}
}
